package com.qf.qfleadnewscommons.scan;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(ScanProperties.class)
@ConditionalOnProperty(value = "scan.enable",havingValue = "true", matchIfMissing = false)
public class ScanAutoConfiguration {

    @Autowired
    private ScanProperties properties;

    @Bean
    public IAcsClient iAcsClient(){
        IClientProfile profile = DefaultProfile
                .getProfile("cn-shanghai", properties.getAccessKeyId(), properties.getAccessKeySecret());
        DefaultProfile
                .addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // 注意：此处实例化的client尽可能重复使用，提升检测性能。避免重复建立连接。
        IAcsClient client = new AdapterAscClient(profile);
        return client;
    }

    @Bean
    public ScanOperator scanOperator(){
        return new ScanOperator();
    }
}
